package com.jeecms.wicool.action.shops;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.shops.DistributionAddress;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.shops.DistributionAddressMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;

import static com.jeecms.common.page.SimplePage.cpn;

/**
 * Created by cuiyanlei on 15/7/15.
 */
@Controller
public class DistributionAddressAct {

    @RequestMapping("/distributionAddress/v_list.do")
    public String list(String city, String district, String address, Integer pageNo, HttpServletRequest request, ModelMap model) {
        CmsUser user = CmsUtils.getUser(request);
        Pagination page = manager.getPage(user.getShopId(), city, district, address, cpn(pageNo), CookieUtils.getPageSize(request));
        model.addAttribute("pagination", page);
        return "shops/list_distribution_address";
    }

    @RequestMapping("/distributionAddress/v_add.do")
    public String add(ModelMap model, HttpServletRequest request) {
        List<ShopInfo> shops = wiShopInfoMng.getAll(CmsUtils.getUser(request));
        model.addAttribute("listShop", shops);
        return "shops/add_distribution_address";
    }

    @RequestMapping("/distributionAddress/o_save.do")
    public String save(Integer shopId, String city, String district, String address, HttpServletRequest request, HttpServletResponse response) {
        ShopInfo shopInfo = wiShopInfoMng.findById(shopId);
        DistributionAddress disAddress = new DistributionAddress(null, shopInfo, city, district, address);
        disAddress.setCreateTime(new Timestamp(System.currentTimeMillis()));
        disAddress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        manager.save(disAddress);
        return "redirect:v_list.do";
    }
    @RequestMapping("/distributionAddress/o_update.do")
    public String update(Integer id, Integer shopId, String city, String district, String address, HttpServletRequest request, HttpServletResponse response) {
        ShopInfo shopInfo = wiShopInfoMng.findById(shopId);
        DistributionAddress disAddress = manager.findById(id);
        disAddress.setShopInfo(shopInfo);
        disAddress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        disAddress.setCity(city);
        disAddress.setDistrict(district);
        disAddress.setAddress(address);
        manager.updateByUpdater(new Updater<DistributionAddress>(disAddress));
        return "redirect:v_list.do";
    }

    @RequestMapping("/distributionAddress/o_delete.do")
    public String delete(Integer[] ids) {
        for (Integer id : ids) {
            manager.deleteById(id);
        }
        return "redirect:v_list.do";
    }
    @RequestMapping("/distributionAddress/v_edit.do")
    public String edit(Integer id, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        CmsUser user = CmsUtils.getUser(request);
        DistributionAddress bean = manager.findById(id);
        model.addAttribute("disAdd", bean);
        model.addAttribute("shopId", user.getShopId());
        model.addAttribute("listShop", wiShopInfoMng.getAll(user));
        return "shops/edit_distribution_address";
    }

    @Autowired
    private WiShopInfoMng wiShopInfoMng;


    @Autowired
    private DistributionAddressMng manager;
}
